package com.example.administrator.waitan.welcome;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.administrator.waitan.R;
import com.example.administrator.waitan.base.BaseActivity;
import com.example.administrator.waitan.main.MainActivity;
import com.mob.tools.gui.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/26.
 */
public class WelcomeActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager mVp;
    private WelcomeViewPagerAdapter mAdapter;
    private ArrayList<View> data;
    private View viewOne, viewTwo, viewThree, viewFour;
    private ImageView[] imageViews;
    private int currentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initView();
        initData();
    }

    private void initView() {
        viewOne = LayoutInflater.from(this).inflate(R.layout.viewpager_welcome_three, null);
        viewTwo = LayoutInflater.from(this).inflate(R.layout.viewpager_welcome_two, null);
        viewThree = LayoutInflater.from(this).inflate(R.layout.viewpager_welcome_one, null);
        viewFour = LayoutInflater.from(this).inflate(R.layout.viewpager_welcome_forth, null);
        viewFour.findViewById(R.id.rl_welcome).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goMain = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(goMain);
                finish();
            }
        });
        mVp = (ViewPager) findViewById(R.id.vp_welcome);
        data = new ArrayList<>();
        mAdapter = new WelcomeViewPagerAdapter(this,data);
    }

    private void initData() {
        mVp.addOnPageChangeListener(this);
        data.add(viewOne);
        data.add(viewThree);
        data.add(viewTwo);
        data.add(viewFour);
        initPoints(data.size());
        mVp.setAdapter(mAdapter);
    }

    private void initPoints(int size) {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_welcome);
        imageViews = new ImageView[size];
        for (int i = 0; i < size; i++) {
            imageViews[i] = (ImageView) linearLayout.getChildAt(i);
            //默认都设为灰色
            imageViews[i].setEnabled(true);
            imageViews[i].setOnClickListener(this);
            imageViews[i].setTag(i);
        }
        currentIndex = 0;
        imageViews[currentIndex].setEnabled(false);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //设置底部小点选中状态
        setCurDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        int position = (Integer) view.getTag();
        setCurView(position);
        setCurDot(position);
    }

    private void setCurView(int position) {
        if (position < 0 || position >= 4) {
            return;
        }
        mVp.setCurrentItem(position);
    }

    /**
     * 设置当前的小点的位置
     */
    private void setCurDot(int position) {
        if (position < 0 || position > 3 || currentIndex == position) {
            return;
        }
        imageViews[position].setEnabled(false);
        imageViews[currentIndex].setEnabled(true);
        currentIndex = position;
    }

}
